// index.js
var common = require('../../utils/userstatus');
var util = require('../../utils/util');
Page({
  data: {
    noonTip: "",
    userInfo: {
      profileImagePath: util.defaultAvatarUrl,
      username: util.defaultUsername
    },
    appInstance: "",
    id: "",
    code: "",
    sharedErrorTips: "",
    sharedSuccess: false
  },
  onLoad(opt) {
    console.log(opt)
    this.getShareInfo(opt);
    var appInstance = getApp();
    this.data.appInstance = appInstance;
    const date = new Date();
    const hour = date.getHours();
    var noonTip;
    if (hour >= 0 && hour < 12) {
      noonTip = '上午好';
    } else if (hour === 12) {
      noonTip = '中午好';
    } else if (hour > 12 && hour < 18) {
      noonTip = '下午好';
    } else {
      noonTip = '晚上好';
    }
    this.setData({
      noonTip: noonTip
    })
  },
  cancel() {
    const token = wx.getStorageSync('accessToken');
    //分享取消，若登录直接跳转到主页
    if (token) {
      wx.switchTab({
        url: '/pages/index/index',
        complete: (com) => {
          if (close) {
            wx.hideLoading();
          }
        }
      })
    } else {
      wx.redirectTo({
        url: '/pages/login/login'
      })
    }
  },
  confirm() {
    console.log(this.data.id)
    const token = wx.getStorageSync('accessToken');
    //若登录，直接跳转
    if (token) {
      wx.setStorageSync('sharedId', this.data.id)
      wx.switchTab({
        url: '/pages/index/index',
        complete: (com) => {
          wx.hideLoading();
        }
      })
    } else {
      //否则到登录页面
      wx.redirectTo({
        url: '/pages/login/login?id=' + this.data.id
      })
    }
  },
  getShareInfo(opt) {
    console.log(opt)
    this.data.code = opt.code;
    this.data.id = opt.id;
    if (this.data.code) {
      this.getUserInfo();
    } else {
      wx.showToast({
        title: '链接异常',
        icon: 'error',
        duration: 2000
      })
      this.setData({
        sharedErrorTips: "链接异常，请重新分享"
      })
    }
  },
  getUserInfo() {
    var url = '/applet/getUserInfoOnFree';
    var requestData = {
      id: this.data.id,
      code: this.data.code
    }
    console.log(requestData)
    util.postRequest(
      url, requestData,
      (data) => {
        console.log(data)
        if (data.code == 2000) {
          var userInfo = data.data
          if (userInfo.profileImagePath &&
            !userInfo.profileImagePath.includes("http")) {
            userInfo.profileImagePath = util.backServerUrl + userInfo.profileImagePath;
          }

          this.setData({
            userInfo: userInfo
          })
          console.log(this.data.userInfo)
          this.setData({
            sharedSuccess: true
          })
        } else {
          wx.showToast({
            title: '链接异常',
            icon: 'error',
            duration: 2000
          })
          this.setData({
            sharedErrorTips: "链接异常，请重新分享"
          })
        }
      },
      (error) => {
        wx.showToast({
          title: '链接异常',
          icon: 'error',
          duration: 2000
        })
        this.setData({
          sharedErrorTips: "链接异常，请重新分享"
        })
      });

  }
})